﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using ByteSite.Common;

namespace ByteSite.BLL
{
    /// <summary>
    ///用户充值表
    /// </summary>
    public partial class user_recharge
    {
        private readonly Entitys.sysconfig sysConfig = new BLL.sysconfig().loadConfig();//获得系统配置信息
        private readonly DAL.Data data;
        public user_recharge()
        {
            data = new DAL.Data();
        }

        #region 基本方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return data.Exists<Entitys.dt_user_recharge>(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Entitys.dt_user_recharge model)
        {
            return data.Insert(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Entitys.dt_user_recharge model)
        {
            return data.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            return data.Delete<Entitys.dt_user_recharge>(id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entitys.dt_user_recharge GetModel(int id)
        {
            return data.Get<Entitys.dt_user_recharge>(id);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Entitys.dt_user_recharge> GetList(int Top, string strWhere, string filedOrder)
        {
            return data.List<Entitys.dt_user_recharge>(Top, strWhere, filedOrder);
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public List<Entitys.dt_user_recharge> GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return data.List<Entitys.dt_user_recharge>(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }
        #endregion

        #region 扩展方法================================
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(int user_id, string user_name, string recharge_no, int payment_id, decimal amount)
        {
            Entitys.dt_user_recharge model = new Entitys.dt_user_recharge();
            model.user_id = user_id;
            model.user_name = user_name;
            model.recharge_no = recharge_no;
            model.payment_id = payment_id;
            model.amount = amount;
            model.status = 0;
            model.add_time = DateTime.Now;
            return data.Insert(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id, string user_name)
        {
            return data.Delete<Entitys.dt_user_recharge>(x => x.id == id && x.user_name == user_name) > 0;
        }

        /// <summary>
        /// 根据单号得到一个对象实体
        /// </summary>
        public Entitys.dt_user_recharge GetModel(string recharge_no)
        {
            return data.Get<Entitys.dt_user_recharge>(x=>x.recharge_no==recharge_no);
        }

        /// <summary>
        /// 直接充值订单
        /// </summary>
        public bool Recharge(Entitys.dt_user_recharge model)
        {
            try
            {
                data.BeginTran();

                Entitys.dt_user_amount_log amountModel = new Entitys.dt_user_amount_log();
                amountModel.user_id = Utils.ObjToInt(model.user_id);
                amountModel.user_name = model.user_name;
                amountModel.value = Utils.ObjToDecimal(model.amount, 0);
                amountModel.remark = "在线充值，单号：" + model.recharge_no;
                amountModel.add_time = DateTime.Now;
                data.Insert<Entitys.dt_user_amount_log>(amountModel);
                data.UpdateColumns<Entitys.dt_users>(x => x.amount == x.amount + amountModel.value, x => x.id == amountModel.user_id);

                model.id = data.Insert<Entitys.dt_user_recharge>(model);
                data.CommitTran();
            }
            catch
            {
                data.RollbackTran();
                return false;
            }
            return true;
        }

        /// <summary>
        /// 确认充值订单
        /// </summary>
        public bool Confirm(string recharge_no)
        {
            try
            {
                data.BeginTran();

                Entitys.dt_user_recharge model = GetModel(recharge_no);

                #region 增加一条账户余额记录===============
                Entitys.dt_user_amount_log amountModel = new Entitys.dt_user_amount_log();
                amountModel.user_id = model.user_id;
                amountModel.user_name = model.user_name;
                amountModel.value = model.amount;
                amountModel.remark = "在线充值，单号：" + recharge_no;
                amountModel.add_time = DateTime.Now;
                data.Insert(amountModel);
                #endregion

                data.UpdateColumns<Entitys.dt_users>(x => x.amount == x.amount + amountModel.value, x => x.id == amountModel.user_id);

                #region 更新充值表=========================
                data.UpdateColumns<Entitys.dt_user_recharge>(x => new Entitys.dt_user_recharge { status = 1, complete_time = DateTime.Now, recharge_no = recharge_no }, x => x.recharge_no == recharge_no);
                #endregion

                data.CommitTran();
            }
            catch
            {
                data.RollbackTran();
                return false;
            }
            return true;
        }

        /// <summary>
        /// 根据充值单号获取支付方式ID
        /// </summary>
        public int GetPaymentId(string recharge_no)
        {
            return Utils.ObjToInt(data.Get<Entitys.dt_user_recharge>(x => x.recharge_no == recharge_no).payment_id, 0);
        }
        #endregion
    }
}